package dang.action.user;

import dang.action.DangAction;
import dang.dao.DaoFactory;
import dang.dao.UserDao;
import dang.entity.Errors;
import dang.entity.User;

public class VerifyAction extends DangAction {
	private String emailVerifyCode;
	private Errors errors=new Errors();
	public String execute() throws Exception {
		if(emailVerifyCode==null||"".equals(emailVerifyCode)){
			errors.setEmailVerifyCode("邮箱验证码错误！");//TODO 出错后不能显示邮箱验证码
			return "fail";
		}
		int idx=emailVerifyCode.lastIndexOf("-")+1;
		int id=Integer.parseInt(emailVerifyCode.substring(idx));
		UserDao dao=DaoFactory.getUserDao();
		User u=dao.findById(id);
		if(u.getEmail_verify_code().equals(emailVerifyCode)){
			//更改激活状态
			u.setIs_email_verify(1);
			dao.updateVerifyState(u);
			session.put("cartInfoId", u.getId());
			session.put("user", u);
			return "success";
		}
		return "fail";
	}
	public String getEmailVerifyCode() {
		return emailVerifyCode;
	}
	public void setEmailVerifyCode(String emailVerifyCode) {
		this.emailVerifyCode = emailVerifyCode;
	}
	public Errors getErrors() {
		return errors;
	}
	public void setErrors(Errors errors) {
		this.errors = errors;
	}
}
